<template>
  <div class="login">
    <div class="loginbox">
      <div class="loginLeft"></div>
      <div class="loginRight">
        <h3 class="title">登录</h3>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >

          <el-form-item
            label="账号"
            prop="username"
            style="margin-top: 46px; margin-bottom: 0px;"
          >
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
              <!-- <svg-icon
                slot="prefix"
                icon-class="user"
                class="el-input__icon input-icon"
              /> -->
            </el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            prop="password"
            style="margin-top: 46px; margin-bottom: 0px;"
          >
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <!-- <svg-icon
                slot="prefix"
                icon-class="password"
                class="el-input__icon input-icon"
              /> -->
            </el-input>
          </el-form-item>
          <!-- <el-form-item prop="code" v-if="captchaOnOff">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>

      <el-checkbox v-model="rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>  -->
          <el-form-item
            style="width: 100%; margin-top: 60px; margin-bottom: 0px;"
          >
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width: 100%"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">

              <router-link class="link-type" :to="'/register'"
                >立即注册</router-link
              >
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script>
import { getCodeImg, BaseInfo } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import MD5 from "md5";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        code: "",
        uuid: "",
      },

      rememberMe: false,
      loginRules: {
        username: [{ trigger: "blur", message: "请输入用户名" }],
        password: [{ trigger: "blur", message: "请输入密码" }],
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    // this.getCode();
    this.getCookie();
    // localStorage.clear()
  },
  methods: {
    // getCode() {
    //   getCodeImg().then(res => {
    //     this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
    //     if (this.captchaOnOff) {
    //       this.codeUrl = "data:image/gif;base64," + res.img;
    //       this.loginForm.uuid = res.uuid;
    //     }
    //   });
    // },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then((res) => {
              // 当前用户拉取user_info信息
              let token = res.data.token;
              localStorage.setItem("token", token);
              const userData = JSON.parse(res.data.userData);
              var params = { userId: userData.userID };
              // // 请求头带入token 再进行其他请求
              BaseInfo(params)
                .then((res) => {
                  let userInfo = res.data || null;
                  if (userInfo) {
                    // this.$message.success(res.msg)
                    localStorage.setItem(
                      "ms_username",
                      this.loginForm.username
                    );
                    let str = JSON.stringify(userInfo);
                    localStorage.setItem("ms_userInfo", str);
                    this.$message.success("登录成功");
                    this.$router.push({ path: this.redirect || "/" });
                  } else {
                    this.$message.error("登录失败，接口返回数据异常！");
                  }
                })
                .catch((err) => {
                  this.loading = false;
                  console.log(err);
                });
            })
            .catch((err) => {
              this.loading = false;
              console.log(err);
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
@import "~@/assets/styles/mixin.scss";
@import "~@/assets/styles/variables.scss";
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
  background: #01072f;
}
.loginbox {
  width: 76.25%;
  height: 75%;
  // background: pink;
}
.loginLeft {
  float: left;
  width: 50%;
  height: 100%;
  background-image: url("../assets/images/loginLeft.png");
  background-size: 100% 100%;
  // border: ;
}
.loginRight {
  float: right;
  width: 50%;
  height: 100%;
  position: relative;
  background-image: url("../assets/images/loginRight.png");
  background-size: 100% 100%;
}
.title {
  position: absolute;
  top: 17.5%;
  left: 17%;
  font-size: 36px;
  color: $base-menu-light-background;
}

.login-form {
  position: absolute;
  top: 30%;
  left: 17%;
  border-radius: 6px;
  background: #ffffff;
  width: 374px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
.el-button {
  height: 7.5%;
  border-radius: 374px;
  background-color: $base-menu-light-background;
  font-size: 20px;
}
</style>

<style scoped>
/deep/ .el-form-item__label {
  font-size: 18px;
  color: #8089bb !important;
  margin-bottom: 3.3%;
}
/deep/ .el-input__inner {
  border: none;
  border-bottom: 1px solid #d8d7d7;
  padding-left: 0px;
}
</style>

<style scoped>
/deep/ .el-form-item__label{
  font-size: 18px;
  color: #8089bb !important;
  margin-bottom: 3.3%;
}
/deep/ .el-input__inner{
  border: none;
  border-bottom:1px solid #d8d7d7 ;
  padding-left:0px;
}

</style>